/*
  全局公共方法
*/

//  将小程序内置非promise api转换为promise
const promisic = function(func) {
    return function(params = {}) {
        return new Promise((resolve, reject) => {
            const args = Object.assign(params, {
                success: res => {
                    resolve(res);
                },
                fail: error => {
                    reject(error);
                }
            });
            func(args);
        });
    };
};

const px2rpx = function(pxNumber) {
    const {
        screenWidth
    } = wx.getSystemInfoSync();
    const rpxNumber = (750 / screenWidth) * pxNumber;
    return rpxNumber;
};

//unix时间戳转换成普通时间
function MyDate(time, type = 0) {
    var d = new Date(time * 1000);
    var Y = d.getFullYear();
    var M = d.getMonth() + 1;
    var D = d.getDate();
    var H = d.getHours();
    var Mi = d.getMinutes();
    var S = d.getSeconds();
    var a = new Array("日", "一", "二", "三", "四", "五", "六");
    var week = new Date().getDay();
    var str = a[week];
    if (type == 0) {
        return Y + '-' + M + '-' + D + ' ' + H + ':' + Mi + ':' + S;
    } else if (type == 1) {
        return Y + '-' + M + '-' + D;
    } else if (type == 2) {
        return H + ':' + Mi + ':' + S;
    } else if (type == 3) {
        return H + ':' + Mi
    }
}

// 获取当前时间和周几
function GetTime() {
    var d = new Date();
    var year = d.getFullYear();
    var month = d.getMonth() + 1;
    var day = d.getDate();
    var hour = d.getHours(); //获取当前小时数(0-23)
    var minute = d.getMinutes();
    var a = new Array("日", "一", "二", "三", "四", "五", "六");
    var week = new Date().getDay();
    var str = a[week];
    return {
        year,
        month,
        day,
        hour,
        minute,
        str
    }
}

//获得随机数
function GetRandom(max) {
    return Math.floor(Math.random() * max);
}
//判断手机号
function IsMobile(mobile) {
    var preg = "^(0|86|17951)?(17[0-9]|13[0-9]|15[012356789]|18[0-9]|14[57])[0-9]{8}$";
    var valid = new RegExp(preg, "gim").test(mobile);
    return valid;
}


//获得当前时间加指定秒数
function GetTimeAdd(second) {
    //  如果需要时分秒，就放开
    var now = new Date();
    var time = now.getTime() + 1000 * second;
    var n2 = new Date(time);
    return n2.getHours() + ':' + n2.getMinutes();
}


function checkMobile() {
    var sMobile = document.mobileform.mobile.value
    if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(sMobile))) {
        // alert("不是完整的11位手机号或者正确的手机号前七位");
        // document.mobileform.mobile.focus();
        return false;
    }
}

//浮点数判断，货币判断len1小数点前位数len2小数点后位数
function IsFloat(float, len1, len2) {
    var preg = "^([0-9]{1," + len1 + "}\.{0,1}[0-9]{0," + len2 + "})$";
    var valid = new RegExp(preg, "gim").test(float);
    return valid;
}
//邮箱判断
function IsEmail(email) {
    var preg = "^\\w+((-\\w+)|(\.\\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$";
    var valid = new RegExp(preg, "gim").test(email);
    return valid;
}
//判断正常名称类的字符串
function IsStrName(str, len1, len2) {
    var preg = "^(\\w|[\\u4E00-\\u9FA5\\uF900-\\uFA2D]|[\\uFF00-\\uFFEF]){" + len1 + "," + len2 + "}$";
    var valid = new RegExp(preg, "gim").test(str);
    return valid;
}
//判断中文字符串
function IsStrZH(str, len1, len2) {
    var preg = "^([\\u4E00-\\u9FA5\\uF900-\\uFA2D]|[\\uFF00-\\uFFEF]){" + len1 + "," + len2 + "}$";
    var valid = new RegExp(preg, "gim").test(str);
    return valid;
}
//判断英文字符串
function IsStrEN(str, len1, len2) {
    var preg = "^([A-Za-z0-9]){" + len1 + "," + len2 + "}$";
    var valid = new RegExp(preg, "gim").test(str);
    return valid;
}
//判断整型数字
function IsNumeric(num, min, max) {
    var preg = "^(\\d){" + min + "," + max + "}$";
    var valid = new RegExp(preg, "gim").test(num);
    return valid;
}
//去左右空格;
function Trim(s) {
    return s.replace(/(^\s*)|(\s*$)/g, "");
}

export {
    promisic,
    px2rpx
}